﻿using CToolsWin.Model;
using Common.Extend;
using DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CToolsWin.DAL
{
    public class DALITellYouResource
    {
        private static DBHelpers db = DBFactorys.GetDBHelper();
        /// <summary>
        /// 初始化表
        /// </summary>
        public void InitTable()
        {
            #region Delete
            DBInit.DeleteTable("ITellYouResource");
            #endregion Delete

            #region Create
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"CREATE TABLE [dbo].[ITellYouResource](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ParentId] [int] NULL,
    [ParentCode] [varchar](100) NULL,
    [LangId] [nvarchar](100) NULL,
    [LangName] [nvarchar](100) NULL,
    [Code] [varchar](100) NULL,
    [Name] [nvarchar](200) NULL,
    [Post] [varchar](100) NULL,
    [ResourceUrl] [varchar](1000) NULL,
	[CreatedTime] DATETIME NOT NULL DEFAULT GETDATE() ,
	[ModifiedTime] DATETIME NULL ,
CONSTRAINT [PK_ITellYouResource] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];");

            db.ExecteNonQueryText(sb.ToString());
            #endregion Create
        }
        /// <summary>
        /// 批量保存list数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="level"></param>
        public void Save(List<ITellYouResource> list)
        {
            if (list.Count > 0)
            {
                ADOHelper.InsertList<ITellYouResource>(list, "ITellYouResource", "Id");
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="level"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<ITellYouResource> QueryList(int level, string code = "")
        {
            string tableName = "ITellYou";
            string sql = string.Format("SELECT * FROM {0} WHERE 1=1{1};", tableName, string.IsNullOrEmpty(code) ? "" : " AND ParentCode=" + code);
            return db.GetTableText(sql)[0].ExToList<ITellYouResource>();
        }
    }
}
